package com.munichosica.myapp.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.sf.jasperreports.data.json.JsonDataAdapter;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.munichosica.myapp.dto.Persona;
import com.munichosica.myapp.dto.PersonaPk;
import com.munichosica.myapp.exceptions.PersonaDaoException;
import com.munichosica.myapp.factory.PersonaDaoFactory;
import com.munichosica.myapp.util.UTFEncodingUtil;

@Controller
@RequestMapping("/Mantenimiento")
public class PersonaController{

	@RequestMapping(value="Persona.jf", method=RequestMethod.GET)
	public String personas(Model model){
		model.addAttribute("persona", new Persona());
		return "tilesMantPersona";
	}
	
	@RequestMapping(value="Listar.jf", method=RequestMethod.POST)
	public @ResponseBody List<Persona> listar(@RequestParam("criterio") String criterio,
											@RequestParam("texto") String texto){
		List<Persona> lista=null;
		try {
			lista=PersonaDaoFactory.create().findByCriterio(criterio,texto);
		} catch (PersonaDaoException e) {
			System.out.println("ERROR: "+e.getMessage());
		}
		return lista;
	}
	
	/*@RequestMapping(value="Nuevo.jf", method=RequestMethod.GET)
	public String nuevo(Model model){
		System.out.println("NUEVA PERSONA");
		Persona persona=new Persona();
		model.addAttribute(persona);
		//return "PersonaForm";
		return "tilesPersonaForm";
	}
	*/
	@RequestMapping(value="Obtener.jf", method=RequestMethod.GET, headers="Accept=*/*")
	public @ResponseBody Persona obtener(@RequestParam("codigo") int codigo){
		Persona persona=null;
		try {
			persona=PersonaDaoFactory.create().findByPrimaryKey(codigo);
		} catch (PersonaDaoException e) {
			System.out.println("ERROR: "+e.getMessage());
		}
		return persona;
	}
	
	@RequestMapping(value="Eliminar.jf", method=RequestMethod.GET)
	public String eliminar(@RequestParam("codigo") int codigo,Model model){
		try {
			PersonaDaoFactory.create().delete(new PersonaPk(codigo));
			model.addAttribute("codigo", codigo);
		} catch (PersonaDaoException e) {
			System.out.println("ERROR: "+e.getMessage());
		}
		return "Success";	
	}
	
	@RequestMapping(value="Procesar.jf", method=RequestMethod.POST)
	public String procesar(Persona persona,Model model){
		try {
			PersonaPk insert = null;
			UTFEncodingUtil.decodeObjectUTF(persona);
			if(persona.getPercodigoI()!=0){
				PersonaDaoFactory.create().update(new PersonaPk(persona.getPercodigoI()), persona);
				model.addAttribute("codigo", persona.getPercodigoI());
			}else{
				insert= PersonaDaoFactory.create().insert(persona);
				model.addAttribute("codigo",insert.getPercodigoI());
			}
		} catch (PersonaDaoException e) {
			System.out.println("ERROR: "+e.getMessage());
			return "Error";
		}
		return "Success";
	}

}
